package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.*;
import com.manger.utils.*;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @ClassName GoodPayCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/8/30 13:52
 * @Version 1.0
 */
public class GoodPayCaseFilesFormat implements CaseFilesFormat{

    private static Map<String, LoanFileEnum> filesMapping = new HashMap<>();

    private List<String> onlyOneFileNameMarks = new ArrayList<>();

    private static String ocrIdCardMark = "被告身份证";

    private Map<String, String> idCardMap = new HashMap<>();

    {
      //  filesMapping.put("被告身份证", LoanFileEnum.PART_IDENTITY_CERTIFICATE);
        onlyOneFileNameMarks.add("人脸识别照片");
        onlyOneFileNameMarks.add("被告身份证");
    }

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++好分期案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        System.out.println("读取委托信息表：" + excelFilePath);
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), GoodPay.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<GoodPay> goodPayList = (List<GoodPay>) map.get(map.keySet().iterator().next());
        if(goodPayList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<GoodPay> errorList = new ArrayList<>();
        for(GoodPay goodPay: goodPayList){
            String dirName = goodPay.getLoanCode() + "_" + goodPay.getUserId() + "_" + goodPay.getLoanUserName();
            String resourcePath = extractDir + "/" + project.getDebtName() + "/" + dirName;
            String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                    + "/" + goodPay.getLoanUserName() + "-" + goodPay.getIdCard();
            try {
                File file = new File(resourcePath);
                if(!file.exists()){
                    continue;
                }
                CopyFiles.createDirs(tempTargetDir);
                File[] tempFiles = file.listFiles();
                for(File tempFile : tempFiles){
                    if (tempFile.isFile() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreFileList)) {
                        continue;
                    }
                    if (tempFile.isDirectory() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreDirList)) {
                        continue;
                    }
                    String targetRealPath = tempTargetDir + "/" + CopyFiles.getLoanFileEnum(tempFile.getName(),filesMapping).getName();
                    if (!CopyFiles.checkHasCopyFileToDirByOnlyOneFileNameMarks(tempFile.getName(), onlyOneFileNameMarks,targetRealPath)) {
                        continue;
                    }
                    CopyFiles.copyFileToDir(tempFile.getAbsolutePath(),targetRealPath );
                    idCardMap.put(goodPay.getIdCard(),tempTargetDir);
                }
                //拷贝统一数据
                CopyFiles.copyBaseFiles(tempTargetDir, project);
                System.out.println("整理原始数据成功：【" + dirName + "】");
            }catch (Exception e){
                e.printStackTrace();
                errorList.add(goodPay);
                System.out.println("整理原始数据失败：【" + dirName + "】");
            }
        }

        //处理数据u idCardMap : 身份证：地址
        CopyFiles.copyOtherMaterialFileToDir(extractDir,idCardMap);

        if(!errorList.isEmpty()){
            String errorFilePath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.ERROR_EXCEL_PRE + System.currentTimeMillis() + ".xlsx";
            ExcelFactory.getExcelFactory().getExportExecutor().sheet("整理错误")
                    .data(errorList).head(GoodPay.class).write(new FileOutputStream(errorFilePath));
            System.out.println("导出整理错误数据【"+errorFilePath+"】");
        }
    }


    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        GoodPayCaseFilesFormat goodPayCaseFilesFormat = new GoodPayCaseFilesFormat();
        String target = SystemConstant.getFormatDir() + "/" + "target" + "/" + "123456";
        String extractDir = "D:\\workSpace\\AutomaticCase\\format\\format1757120344000/好分期";
        goodPayCaseFilesFormat.format(extractDir,"C:\\Users\\liuhj\\Desktop\\caseTest",ProjectUtils.projectMap.get(DetNameEnum.GOOD_PAY));
    }
}
